#include "updatestudentinfo.h"
#include "ui_updatestudentinfo.h"
#include <string>
using namespace std;

UpdateStudentInfo::UpdateStudentInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UpdateStudentInfo)
{
    ui->setupUi(this);
    this->setWindowIcon(QIcon(":/img/student.jpg"));
    this->psm = ManagementSystem::GetInstance();
    this->stuInfo = nullptr;
}

UpdateStudentInfo::~UpdateStudentInfo()
{
    delete ui;
}
void UpdateStudentInfo::setInfo()
{
    ui->nameEdit->setText(QString::fromStdString(this->stuInfo->getName()));
    ui->stuidEdit->setText(QString::fromStdString(this->stuInfo->getStuId()));
    ui->gradeEdit->setText(QString::fromStdString(this->stuInfo->getGrade()));
    ui->pidEdit->setText(QString::fromStdString(this->stuInfo->getPId()));
    ui->scoreEdit->setText(QString::number(this->stuInfo->getScore()));
    ui->creditEdit->setText(QString::number(this->stuInfo->getCredit()));
    ui->userEdit->setText(QString::fromStdString(this->stuInfo->getUsername()));
    ui->pswEdit->setText(QString::fromStdString(this->stuInfo->getPassword()));
}


void UpdateStudentInfo::on_submit_clicked()
{
    //查看修改项
    // 首先查看这四个属性，因为这四个属性修改需要对红黑树修改
    string oldStuId = this->stuInfo->getStuId();
    string newStuId = ui->stuidEdit->text().toStdString();
    double oldScore = this->stuInfo->getScore();
    double newScore = ui->scoreEdit->text().toDouble();
    double oldCredit = this->stuInfo->getCredit();
    double newCredit = ui->creditEdit->text().toDouble();
    string oldUsername = this->stuInfo->getUsername();
    string newUsername = ui->userEdit->text().toStdString();
    // 创建临时变量，用来做比较
    StudentInfo tsi;
    // 判断是否可以更新（防止学号或用户名重复）
    bool update = true;
    if (update && oldStuId != newStuId) // 学号被修改了
    {
        cout << oldStuId << "!=" << newStuId << endl;
        cout << "update stuId" << endl;
        tsi.setStuId(newStuId);
        // 检查新学号是否已存在
        bool repeat = this->psm->rbStuId->search(&tsi);
        if (repeat) // 学号重复
        {
            cout << "学号重复" << endl;
            update = false;
            QMessageBox::critical(this,
                   tr("更新失败"),
                   tr("新输入学号重复"),
                   QMessageBox::Ok | QMessageBox::Cancel,
                   QMessageBox::Ok);
        }
    }
    if (update && oldUsername != newUsername)
    {
        cout << "用户名修改" << endl;
        tsi.setUsername(newUsername);
        bool repeat = this->psm->rbUsername->search(&tsi);
        if (repeat)
        {
            update = false;
            cout << "用户名重复" << endl;
            QMessageBox::critical(this,
                   tr("更新失败"),
                   tr("新输入用户名重复"),
                   QMessageBox::Ok | QMessageBox::Cancel,
                   QMessageBox::Ok);
        }
    }
    if (update) // 此时学号无重复或无修改，并且用户名无重复或无修改
    {
        cout << "refresh" << endl;
        if (oldStuId != newStuId) // 学号有修改的情况（修改了但没重复）
        {
//            this->psm->rbStuId->search(psm->rbStuId->root, this->stuInfo);

            this->psm->rbStuId->RemoveNode(stuInfo);
            stuInfo->setStuId(newStuId);
            this->psm->rbStuId->Insert(stuInfo);
        }
        if (oldUsername != newUsername) // 用户名有修改的情况（修改了但没重复）
        {
            this->psm->rbUsername->RemoveNode(stuInfo);
            stuInfo->setUsername(newUsername);
            this->psm->rbUsername->Insert(stuInfo);
        }
        if (oldScore != newScore) // 判断已修学分是否被修改
        {
            this->psm->rbScore->RemoveNode(stuInfo);
            stuInfo->setScore(newScore);
            this->psm->rbScore->Insert(stuInfo);
        }
        if (oldCredit != newCredit) // 判断平均绩点是否被修改
        {
            this->psm->rbCredit->RemoveNode(stuInfo);
            stuInfo->setCredit(newCredit);
            this->psm->rbCredit->Insert(stuInfo);
        }
        // 下面这些字段不管改没改都更新一遍
        this->stuInfo->setName(ui->nameEdit->text().toStdString());
        this->stuInfo->setPId(ui->pidEdit->text().toStdString());
        this->stuInfo->setGrade(ui->gradeEdit->text().toStdString());
        this->stuInfo->setPassword(ui->pswEdit->text().toStdString());
        QMessageBox::information(this,
               tr("更新成功"),
               tr("更新成功"),
               QMessageBox::Ok | QMessageBox::Cancel,
               QMessageBox::Ok);
        this->hide();
    }

}

// 获取学生信息
void UpdateStudentInfo::getInfo(StudentInfo* s1)
{
    this->stuInfo = s1;
//    cout << "get Info" << this->stuInfo->stuId << endl;
    setInfo();
}
